//
//  BalanceMasterViewController.m
//  Balance
//
//  Created by Calvin Kuo on 12-03-22.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BalanceMasterViewController.h"

#import "BalanceDetailViewController.h"
#import "CEIViewController.h"

@implementation BalanceMasterViewController

@synthesize detailViewController = _detailViewController;
@synthesize metrics = _metrics;


- (void)awakeFromNib
{
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    [super awakeFromNib];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
     _metrics = [NSArray arrayWithObjects:@"Count of Work Items", @"Collections Effectiveness Index", @"Amount Overdue", @"Strategy Effectiveness", @"Average Days Delinquent", @"Percent Current", @"Percent 91+ Days Delinquent", @"Collection Score", nil];
    
    
    
    self.detailViewController = (BalanceDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    
    //default to select the 3rd row to display Amount Overdue first 
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    return [_metrics count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MetricCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSString *metric = [_metrics objectAtIndex:indexPath.row];
    cell.textLabel.text = metric;
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];
  
    /*
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
*/
        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        
    UIViewController *detailVC = nil;
     
    //CEI
    if (indexPath.row == 1){
        UINavigationController *navigationController =
        [self.storyboard instantiateViewControllerWithIdentifier:
         @"CEIViewController"];
        
        detailVC =[[navigationController viewControllers] objectAtIndex:0];
    }
    
    //amount overdue
    if (indexPath.row == 2){
        UINavigationController *navigationController =
        [self.storyboard instantiateViewControllerWithIdentifier:
         @"BalanceDetailViewController"];
        
        detailVC =[[navigationController viewControllers] objectAtIndex:0];
        
        
    }
    
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailVC, nil];
    
    self.splitViewController.viewControllers = viewControllers;
    
    /* to present the VC programmatically and directly
    //[self presentViewController:navigationController animated:YES
                     completion:nil];
    */    
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

@end
